package com.dalin.platform.autocode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.dalin.common.PropertyConst;
import com.dalin.common.clazz.ArgList;
import com.dalin.common.clazz.Clazz;
import com.dalin.common.clazz.ImportList;
import com.dalin.common.clazz.Interfazz;
import com.dalin.common.clazz.Method;
import com.dalin.common.clazz.Note;
import com.dalin.common.clazz.Property;
import com.dalin.common.clazz.PropertyList;
import com.dalin.common.utils.FileUtils;
import com.dalin.common.utils.NumberUtils;
import com.dalin.common.utils.StringUtils;
import com.dalin.common.utils.TextUtils;
import com.dalin.framework.dao.ConnectFactory;
import com.dalin.framework.dao.DbConn;
import com.dalin.framework.vos.ColumnBaseInfoVo;
import com.dalin.framework.vos.JdbcVo;

/*****************************************************************************
 * @content: 3.0 *
 * @author : 那建林 *
 * @crtDate: 2009-07-27 *
 ****************************************************************************/
public final class SkyJpa {
	private static String NEXT_NJL_ID = "njlId27";

	public static String getNextNjlId() {
		return SkyJpa.NEXT_NJL_ID = NumberUtils
				.getNextStrNum(SkyJpa.NEXT_NJL_ID);
	}

	/*************************************************************************
	 * @content: 根据属性生成它的get/set方法
	 * @param : propList 属性集合
	 * @return : List<Method>
	 * @author : 那建林
	 * @crtDate: 2009-09-17
	 * @modDate: 2010-05-11
	 * @version: 1.3
	 */
	public static List<Method> getGetSetMethodList(PropertyList propList) {
		List<Method> methodList = new ArrayList<Method>();
		Method method = null;
		StringBuffer buf = null;
		for (Property p : propList.getAll()) {
			String pType = p.getType();
			String pName = p.getName();
			String upperPName = pName.substring(0, 1).toUpperCase()
					+ pName.substring(1);
			// getMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
					.append(PropertyConst.RETURN_STR)
					.append(PropertyConst.SPACE).append(pName)
					.append(PropertyConst.SEMICOLON);
			method = new Method(PropertyConst.GET_STR + upperPName);
			method.setBody(buf.toString());
			method.setRtnType(pType);
			methodList.add(method);

			// setMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
					.append(PropertyConst.THIS_STR).append(PropertyConst.DOT)
					.append(pName).append(PropertyConst.SPACE)
					.append(PropertyConst.EQUAL).append(PropertyConst.SPACE)
					.append(pName).append(PropertyConst.SEMICOLON);
			ArgList argList = new ArgList();
			argList.add(pType, pName);
			method = new Method(PropertyConst.SET_STR + upperPName);
			method.setArgList(argList);
			method.setBody(buf.toString());
			methodList.add(method);
		}
		return methodList;
	}

	/*************************************************************************
	 * @content: 根据表名生成普通的映射javabean,属性及相应的get/set方法
	 * @param : jdbc 数据库连接bean
	 * @param : tableName 表名
	 * @param : packageName 包名
	 * @return : Clazz
	 * @author : 那建林
	 * @crtDate: 2009-10-03
	 * @modDate: 2010-05-11
	 * @version: 1.3
	 * @throws Exception
	 */
	public static Clazz getJavaClazz(DbConn conn, String tableName,
			String packageName) throws Exception {
		Set<ColumnBaseInfoVo> list = conn.queryColumnInfosByTable(tableName);
		PropertyList propList = new PropertyList();

		for (ColumnBaseInfoVo vo : list) {
			String lowerName = StringUtils.underlineToLowerCamelCase(vo
					.getColumnName());
			String javaType = conn.getJavaTypeFromColumnType(
					vo.getColumnType(), vo.getColumnLength(),
					vo.getDecimalDigits());
			StringBuffer noteBuf = new StringBuffer();
			noteBuf.append(vo.getColumnName()).append(PropertyConst.COMMA)
					.append(vo.getColumnLength()).append(PropertyConst.COMMA)
					.append(vo.getDecimalDigits());
			propList.add(javaType, lowerName, noteBuf.toString());
		}

		Clazz myClass = new Clazz();
		myClass.setPackageName(packageName);
		myClass.setClassName(StringUtils.underlineToUpperCamelCase(tableName)
				+ "VO");
		myClass.setPropertyList(propList);
		myClass.setMethodList(getGetSetMethodList(propList));
		return myClass;
	}

	/*************************************************************************
	 * @content: 根据表名生成JPA注解映射bean
	 * @param : jdbc 数据库连接bean
	 * @param : tableName 表名
	 * @param : packageName 包名
	 * @return : Clazz
	 * @author : 那建林
	 * @crtDate: 2009-10-03
	 * @modDate: 2010-05-11
	 * @version: 1.3
	 * @throws Exception
	 */
	public static Clazz getAnnotationClazz(DbConn conn, String tableName,
			String packageName) throws Exception {
		Set<ColumnBaseInfoVo> list = conn.queryColumnInfosByTable(tableName);
		PropertyList propList = new PropertyList();

		for (ColumnBaseInfoVo vo : list) {
			String lowerName = StringUtils.underlineToLowerCamelCase(vo
					.getColumnName());
			String javaType = conn.getJavaTypeFromColumnType(
					vo.getColumnType(), vo.getColumnLength(),
					vo.getDecimalDigits());
			StringBuffer noteBuf = new StringBuffer();
			noteBuf.append(vo.getColumnName()).append(PropertyConst.COMMA)
					.append(vo.getColumnLength()).append(PropertyConst.COMMA)
					.append(vo.getDecimalDigits());
			propList.add("@Column(name=\"" + vo.getColumnName() + "\")",
					javaType, lowerName, noteBuf.toString());
		}

		Clazz myClass = new Clazz();
		myClass.setPackageName(packageName);
		myClass.setClassName(StringUtils.underlineToUpperCamelCase(tableName)
				+ "Entity");
		myClass.setExtendsClassName("BaseEntity");
		myClass.setPropertyList(propList);
		myClass.setMethodList(getGetSetMethodList(propList));
		return myClass;
	}

	/*************************************************************************
	 * @content: 根据表名生成condition查询封装类
	 * @param : jdbc 数据库连接bean
	 * @param : tableName 表名
	 * @param : packageName 包名
	 * @return : Clazz
	 * @author : 那建林
	 * @crtDate: 2009-10-03
	 * @modDate: 2010-05-27
	 * @version: 1.3
	 */
	public static Clazz getConditionClazz(DbConn conn, String tableName,
			String packageName) throws Exception {
		Clazz cls = getJavaClazz(conn, tableName, packageName);
		// 引用哪些包
		// 类名
		cls.setClassName(StringUtils.underlineToUpperCamelCase(tableName)
				+ "Cond");
		cls.setExtendsClassName("Condition");

		List<Method> methodList = cls.getMethodList();
		/**
		 * 增加 protected void prepareParams()方法
		 */
		Method preMethod = new Method("prepareParams");
		preMethod.setRtnType(cls.getClassName());
		StringBuffer bodyBuf = new StringBuffer(); // 方法体
		bodyBuf.append(PropertyConst.TWO_TAB)
				.append("setBaseQueryStr(\"from ")
				.append(StringUtils.underlineToUpperCamelCase(tableName)
						+ "Entity").append(" h\");")
				.append(PropertyConst.LINE_SEPARATOR);
		PropertyList proList = cls.getPropertyList();
		PropertyList newPropList = new PropertyList();
		// 先组织查询语句，再把类属性全变为String类型
		for (Property p : proList.getAll()) {
			String propType = p.getType();
			String propName = p.getName();
			if (PropertyConst.STRING_STR.equals(propType)) {
				bodyBuf.append(PropertyConst.TWO_TAB).append("setParam(\"h.")
						.append(propName).append("\", \"like\", \"%\" + ")
						.append(propName).append(" + \"%\");")
						.append(PropertyConst.LINE_SEPARATOR);
				newPropList.add(p);
			} else {
				bodyBuf.append(PropertyConst.TWO_TAB).append("setParam(\"h.")
						.append(propName).append("\", \">=\", ");
				if (PropertyConst.DOUBLE_STR.equals(propType)) {
					bodyBuf.append("StringUtils.getDoubleFromStrNum(");
				} else if (PropertyConst.DATE_STR.equals(propType)) {
					bodyBuf.append("DateUtils.getDate(");
				}
				bodyBuf.append(propName + 1).append("));")
						.append(PropertyConst.LINE_SEPARATOR);
				p.setName(propName + 1);
				newPropList.add(p);
				bodyBuf.append(PropertyConst.TWO_TAB).append("setParam(\"h.")
						.append(propName).append("\", \"<\", ");
				if (PropertyConst.DOUBLE_STR.equals(propType)) {
					bodyBuf.append("StringUtils.getDoubleFromStrNum(");
				} else if (PropertyConst.DATE_STR.equals(propType)) {
					bodyBuf.append("DateUtils.getDate(");
				}
				bodyBuf.append(propName + 2).append("));")
						.append(PropertyConst.LINE_SEPARATOR);
				p.setName(propName + 2);
				newPropList.add(p);
			}
		}
		cls.setPropertyList(newPropList);
		bodyBuf.append(PropertyConst.TWO_TAB).append("return this;");
		preMethod.setBody(bodyBuf.toString());
		methodList.add(preMethod);
		cls.setMethodList(methodList);
		return cls;
	}

	/*************************************************************************
	 * @content: 根据表名生成创建service接口类
	 * @param : jdbc 数据库连接bean
	 * @param : tableName 表名
	 * @param : packageName 包名
	 * @return : Interfazz
	 * @author : 那建林
	 * @crtDate: 2009-10-03
	 * @modDate: 2010-05-30
	 * @version: 1.3
	 */
	public static Interfazz getServiceInterfaceClazz(String tableName,
			String packageName) throws Exception {
		String javaTableName = StringUtils.underlineToUpperCamelCase(tableName);
		Interfazz inter = new Interfazz();
		inter.setInterfaceName(javaTableName + "Service");
		List<Method> methodList = new ArrayList<Method>();
		Method method = null;
		ArgList argList = new ArgList();
		/**
		 * 查询方法
		 */
		method = new Method("query" + javaTableName + "s");
		method.setRtnType("List");
		argList.add(javaTableName + "Condition", "cond");
		method.setArgList(argList);
		method.setMethodNote(Note.getMethodNote(method));
		method.setException("Exception");
		methodList.add(method);
		/**
		 * 查找单条记录方法
		 */
		method = new Method("find" + javaTableName + "ById");
		method.setRtnType(javaTableName + "DTO");
		argList = new ArgList();
		argList.add("Integer", "id");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 增加记录方法
		 */
		method = new Method("add" + javaTableName);
		argList = new ArgList();
		argList.add(javaTableName + "VO", "vo");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 修改记录方法
		 */
		method = new Method("modify" + javaTableName);
		argList = new ArgList();
		argList.add(javaTableName + "VO", "vo");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 删除方法
		 */
		method = new Method("delete" + javaTableName + "s");
		argList = new ArgList();
		argList.add("Integer[]", "idAry");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);

		inter.setMethodList(methodList);
		return inter;
	}

	public static Clazz getServiceImpleClazz(DbConn conn, String tableName,
			String packageName) throws Exception {
		String javaTableName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propList = getJavaClazz(conn, tableName, packageName)
				.getPropertyList();
		Interfazz inter = getServiceInterfaceClazz(tableName, packageName);
		Clazz cls = new Clazz();
		cls.setPackageName(packageName);
		cls.setClassName(inter.getInterfaceName() + "Impl");
		cls.setExtendsClassName("BaseServiceImpl");
		List<String> implementsList = new ArrayList<String>();
		implementsList.add(inter.getInterfaceName());
		cls.setImplementsList(implementsList);
		cls.setMethodList(inter.getMethodList());

		Method method = null;
		StringBuffer buf = null;
		/**
		 * 查询方法
		 */
		method = cls.getMethodList().get(0);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append("        List<" + javaTableName + "DTO> list = (List<"
				+ javaTableName + "DTO>) getDbDao().selectList(\""
				+ javaTableName + "Entity.selectList\", cond);");
		buf.append("\n        return list;");
		method.setBody(buf.toString());
		cls.getMethodList().set(0, method);
		/**
		 * 查找单条记录方法
		 */
		method = cls.getMethodList().get(1);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "DTO")
				.append(" enty = (").append(javaTableName + "DTO")
				.append(") getDao().findObjById(")
				.append(javaTableName + "DTO.class, id);")
				.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "DTO")
				.append(" vo = new ").append(javaTableName).append("VO();")
				.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("return DTO;");
		method.setBody(buf.toString());
		cls.getMethodList().set(1, method);
		/**
		 * 增加记录方法
		 */
		method = cls.getMethodList().get(2);
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "Entity")
				.append(" enty = new ").append(javaTableName + "Entity();")
				.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("enty = get")
				.append(javaTableName).append("EntityValue(enty, vo);")
				.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("getDao().createObj(enty);");
		method.setBody(buf.toString());
		cls.getMethodList().set(2, method);
		/**
		 * 修改记录方法
		 */
		method = cls.getMethodList().get(3);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append("Long id = vo.get")
				.append(javaTableName + "Id();")
				.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "Entity")
				.append(" enty = (").append(javaTableName + "Entity")
				.append(") getDao().findObjById(")
				.append(javaTableName + "Entity.class, id);")
				.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("enty = get")
				.append(javaTableName).append("EntityValue(enty, vo);")
				.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB)
				.append("getDao().updateObjNbv(enty);");
		method.setBody(buf.toString());
		cls.getMethodList().set(3, method);
		/**
		 * 批量删除方法
		 */
		method = cls.getMethodList().get(4);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append("getDao().deleteObj(")
				.append(javaTableName).append("Entity.class, idArray);");
		method.setBody(buf.toString());
		cls.getMethodList().set(4, method);
		/**
		 * 增加属性值copy方法
		 */
		method = new Method("get" + javaTableName + "EntityValue");
		method.setRtnType(javaTableName + "Entity");
		method.setModifier(PropertyConst.PRIVATE_STR);
		ArgList argList = new ArgList();
		argList.add(javaTableName + "Entity", "enty");
		argList.add(javaTableName + "VO", "vo");
		method.setArgList(argList);
		method.setBody(setPropertyValue(propList, "enty", "vo")
				+ "\n        return enty");
		cls.getMethodList().add(method);
		return cls;
	}

	public static Clazz getControllerClazz(String serviceName,
			String tableName, String packageName) throws Exception {
		String serviceRefName = serviceName.substring(0, 1).toLowerCase()
				+ serviceName.substring(1);
		String upperJavaName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propertyList = new PropertyList();
		propertyList.add(new Property("@Resource", serviceName, serviceRefName, null));
		List<Method> methodList = new ArrayList<Method>();
		Method method = new Method("describle" + upperJavaName + "s");
		ArgList argList = new ArgList();
		argList.add(StringUtils.underlineToUpperCamelCase(tableName) + "Cond",
				"cond");
		argList.add("BindingResult", "result");
		method.setArgList(argList);
		method.setMethodNote("@RequestMapping(\"/" + "describle"
				+ upperJavaName + "s" + "\")\n    @ResponseBody");
		method.setRtnType("Object");
		StringBuffer buf = new StringBuffer();
		buf.append("        try{\n");
		buf.append(PropertyConst.THREE_TAB).append(
				"return getSuccessValue(najlService.describe" + upperJavaName
						+ "s(cond));\n");
		buf.append(PropertyConst.TWO_TAB).append("} catch (Exception e) {\n");
		buf.append(PropertyConst.THREE_TAB).append("e.printStackTrace();\n");
		buf.append(PropertyConst.THREE_TAB).append("log.error(\"\", e);\n");
		buf.append(PropertyConst.THREE_TAB).append(
				"return getErrorDTO(result);\n");
		buf.append(PropertyConst.TWO_TAB).append("}");
		method.setBody(buf.toString());
		methodList.add(method);

		method = new Method("add" + upperJavaName);
		argList = new ArgList();
		argList.add(StringUtils.underlineToUpperCamelCase(tableName) + "VO",
				"vo");
		argList.add("BindingResult", "result");
		method.setArgList(argList);
		method.setMethodNote("@RequestMapping(\"/" + "add" + upperJavaName
				+ "\")\n    @ResponseBody");
		method.setRtnType("Object");
		buf = new StringBuffer();
		buf.append("        try{\n");
		buf.append(PropertyConst.THREE_TAB).append(
				"najlService.add" + upperJavaName + "(vo);\n");
		buf.append(PropertyConst.TWO_TAB).append("} catch (Exception e) {\n");
		buf.append(PropertyConst.THREE_TAB).append("e.printStackTrace();\n");
		buf.append(PropertyConst.THREE_TAB).append("log.error(\"\", e);\n");
		buf.append(PropertyConst.THREE_TAB).append(
				"return getErrorDTO(result);\n");
		buf.append(PropertyConst.TWO_TAB).append("}\n");
		buf.append(PropertyConst.TWO_TAB).append(
				"return getSuccessValue(\"OK\");");
		method.setBody(buf.toString());
		methodList.add(method);

		method = new Method("modify" + upperJavaName);
		argList = new ArgList();
		argList.add(StringUtils.underlineToUpperCamelCase(tableName) + "VO",
				"vo");
		argList.add("BindingResult", "result");
		method.setArgList(argList);
		method.setMethodNote("@RequestMapping(\"/" + "modify" + upperJavaName
				+ "\")\n    @ResponseBody");
		method.setRtnType("Object");
		buf = new StringBuffer();
		buf.append("        try{\n");
		buf.append(PropertyConst.THREE_TAB).append(
				"najlService.modify" + upperJavaName + "(vo);\n");
		buf.append(PropertyConst.TWO_TAB).append("} catch (Exception e) {\n");
		buf.append(PropertyConst.THREE_TAB).append("e.printStackTrace();\n");
		buf.append(PropertyConst.THREE_TAB).append("log.error(\"\", e);\n");
		buf.append(PropertyConst.THREE_TAB).append(
				"return getErrorDTO(result);\n");
		buf.append(PropertyConst.TWO_TAB).append("}\n");
		buf.append(PropertyConst.TWO_TAB).append(
				"return getSuccessValue(\"OK\");");
		method.setBody(buf.toString());
		methodList.add(method);

		method = new Method("delete" + upperJavaName+"s");
		argList = new ArgList();
		argList.add("Integer[]", "idAry");
		argList.add("BindingResult", "result");
		method.setArgList(argList);
		method.setMethodNote("@RequestMapping(\"/" + "delete" + upperJavaName
				+ "s\")\n    @ResponseBody");
		method.setRtnType("Object");
		buf = new StringBuffer();
		buf.append("        try{\n");
		buf.append(PropertyConst.THREE_TAB).append(
				"najlService.delete" + upperJavaName + "s(idAry);\n");
		buf.append(PropertyConst.TWO_TAB).append("} catch (Exception e) {\n");
		buf.append(PropertyConst.THREE_TAB).append("e.printStackTrace();\n");
		buf.append(PropertyConst.THREE_TAB).append("log.error(\"\", e);\n");
		buf.append(PropertyConst.THREE_TAB).append(
				"return getErrorDTO(result);\n");
		buf.append(PropertyConst.TWO_TAB).append("}\n");
		buf.append(PropertyConst.TWO_TAB).append(
				"return getSuccessValue(\"OK\");");
		method.setBody(buf.toString());
		methodList.add(method);

		List<Method> getSetMethodList = buildGetSetMethodByProperty(propertyList);
		for (Method m : getSetMethodList) {
			methodList.add(m);
		}

		Clazz cls = new Clazz();
		cls.setNote(cls.getNote()
				+ "\n@Controller\n@RequestMapping(\"/发布路径\")\n");
		cls.setPackageName(packageName);
		ImportList importList = new ImportList();
		importList.add("java.util.ArrayList;");
		importList.add("java.util.List;");
		importList
				.add("org.springframework.beans.factory.annotation.Autowired;");
		importList.add("org.springframework.stereotype.Controller;");
		importList
				.add("org.springframework.web.bind.annotation.RequestMapping;");
		importList.add("org.springframework.web.bind.annotation.RequestParam;");
		importList.add("org.springframework.web.bind.annotation.ResponseBody;");
		importList.add("com.skyform.api.base.BaseController;");
		cls.setImportList(importList);
		cls.setClassName(upperJavaName + "Controller");
		cls.setExtendsClassName("BaseController");
		cls.setPropertyList(propertyList);
		cls.setMethodList(methodList);
		return cls;
	}

	// 增删查改
	public static void crudAndExts(DbConn conn, String tableName,
			String packageName) throws Exception {
		String savedDir = "/tmp/crud/" + tableName + "/";
		File f = new File(savedDir);
		FileUtils.deleteFiles(f);
		f.mkdirs();

		// DTO值对象
		Clazz myClazz = getJavaClazz(conn, tableName, packageName);
		myClazz.setClassName(myClazz.getClassName().replace("VO", "DTO"));
		TextUtils.overwriteToText(myClazz.toString(), new File(savedDir
				+ myClazz.getClassName().replace("VO", "DTO") + ".java"));

		// VO值对象
		myClazz = getJavaClazz(conn, tableName, packageName);
		myClazz.setClassName(myClazz.getClassName().replace("Sf", ""));
		TextUtils.overwriteToText(myClazz.toString(), new File(savedDir
				+ myClazz.getClassName() + ".java"));
		// Annotation值对象
		myClazz = getAnnotationClazz(conn, tableName, packageName + ".entity");
		myClazz.setClassName(myClazz.getClassName().replace("Sf", ""));
		TextUtils.overwriteToText(myClazz.toString(), new File(savedDir
				+ myClazz.getClassName() + ".java"));

		// Condition查询条件对象
		myClazz = getConditionClazz(conn, tableName, packageName);
		myClazz.setClassName(myClazz.getClassName().replace("Sf", ""));
		TextUtils.overwriteToText(myClazz.toString(), new File(savedDir
				+ myClazz.getClassName() + ".java"));

		// service接口类
		Interfazz fazz = getServiceInterfaceClazz(tableName, packageName);
		TextUtils.overwriteToText(fazz.toString(),
				new File(savedDir + fazz.getInterfaceName() + ".java"));

		// service接口实现类
		myClazz = getServiceImpleClazz(conn, tableName, packageName);
		TextUtils.overwriteToText(myClazz.toString(), new File(savedDir
				+ myClazz.getClassName() + ".java"));

		// 增加修改action类
		myClazz = getControllerClazz("CustomerManageService", tableName,
				packageName);
		TextUtils.overwriteToText(myClazz.toString(), new File(savedDir
				+ myClazz.getClassName() + ".java"));
	}

	// 把一个类的属性赋给另一个类的属性
	public static String setPropertyValue(PropertyList propList, String setRef,
			String getRef) {
		StringBuffer buf = new StringBuffer();
		for (Property p : propList.getAll()) {
			String pName = p.getName();
			pName = pName.substring(0, 1).toUpperCase() + pName.substring(1);
			buf.append(PropertyConst.TWO_TAB).append(setRef).append(".set")
					.append(pName).append("(").append(getRef).append(".get")
					.append(pName).append("()); // ")
					.append(PropertyConst.LINE_SEPARATOR);
		}
		return buf.toString();
	}

	/*************************************************************************
	 * @content: 根据属性生成它的get/set方法
	 * @param : propList 属性集合
	 * @return : List<Method>
	 * @author : 那建林
	 * @crtDate: 2009-09-17
	 */
	public static List<Method> buildGetSetMethodByProperty(PropertyList propList) {
		List<Method> methodList = new ArrayList<Method>();
		Method method = null;
		StringBuffer buf = null;
		for (Property p : propList.getAll()) {
			String pType = p.getType();
			String pName = p.getName();
			String upperPName = pName.substring(0, 1).toUpperCase()
					+ pName.substring(1);
			// getMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
					.append(PropertyConst.RETURN_STR)
					.append(PropertyConst.SPACE).append(pName)
					.append(PropertyConst.SEMICOLON)
					.append(PropertyConst.LINE_SEPARATOR);
			method = new Method(PropertyConst.GET_STR + upperPName);
			method.setBody(buf.toString());
			method.setRtnType(pType);
			methodList.add(method);

			// setMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
					.append(PropertyConst.THIS_STR).append(PropertyConst.DOT)
					.append(pName).append(PropertyConst.SPACE)
					.append(PropertyConst.EQUAL).append(PropertyConst.SPACE)
					.append(pName).append(PropertyConst.SEMICOLON)
					.append(PropertyConst.LINE_SEPARATOR);
			ArgList argList = new ArgList();
			argList.add(pType, pName);
			method = new Method(PropertyConst.SET_STR + upperPName);
			method.setArgList(argList);
			method.setBody(buf.toString());
			methodList.add(method);
		}
		return methodList;
	}

	/**
	 * 
	 */
	public static void buildSkyFormMybatisCode() {
		JdbcVo jdbc = new JdbcVo();
		jdbc.setDriverClassName(PropertyConst.DRIVER_MYSQL);
		jdbc.setHostIp("10.10.214.100"); // 主机IP
		jdbc.setPort("3306"); // 端口号
		jdbc.setDbName("skyform_development_fdc"); // 数据库名
		jdbc.setUserName("skycloud"); // 用户名
		jdbc.setUserPswd("skycloud"); // 密码
		DbConn conn;
		try {
			conn = ConnectFactory.getConnectionInstance(jdbc);
			Set<String> tts = conn.queryTables();
			for (String s : tts) {
					System.out.println(s);
					buildSkyFormMybatisCode1(conn, s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void buildSkyFormMybatisCode1(DbConn conn, String tableName) {
		String key = "ID";
		String keyc = "id";
		String ucc = StringUtils.underlineToUpperCamelCase(tableName);
		try {
			crudAndExts(conn, tableName,
					"com.skyform.teraproc.cloudlogic.customer");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		buildSkyFormMybatisCode();
	}
}